﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PharmacyPharnet.Core;
using Pharmacy.ExceptionHandling;
using PharmacyPharnet.Core.CustomEventArgs;

namespace PharmacyPharnet.KinhDoanh
{
    public partial class FrmCalculateDiscountAndVAT : DialogViewBase
    {
        #region Declarations
        public decimal TotalAmountBeforeVAT { get; set; }
        public decimal TotalAmount { get; set; }
        public decimal DiscountAmount { get; set; }
        public decimal DiscountRate { get; set; }
        public decimal VATRate { get; set; }
        public decimal VATAmount { get; set; }

        bool needToCalculateVATAmount = true;
        bool needToCalculateDiscountAmount = true;
        bool needToCalculateVATRate = false;
        bool needToCalculateDiscountRate = false;

        public delegate void OnSubmittedForm(object sender, DiscountAndVATArgs e);
        public event OnSubmittedForm SubmittedForm;

        #endregion

        #region Constructors
        public FrmCalculateDiscountAndVAT()
        {
            InitializeComponent();
        }        
        #endregion

        #region Override Methods
        
        #endregion

        #region Custom Methods
        private void SetDefaultValue()
        {
            needToCalculateDiscountRate = true;
            needToCalculateVATRate = true;
            txtTotalAmoutBeforeVAT.EditValue = TotalAmountBeforeVAT;
            txtTotalAmount.EditValue = TotalAmount;
            spnDiscountAmount.EditValue = DiscountAmount;
            spnVATAmount.EditValue = VATAmount;
            needToCalculateDiscountRate = false;
            needToCalculateVATRate = false;
        }

        private void ReCalculateTotalAmount()
        {
            txtTotalAmount.EditValue = TotalAmountBeforeVAT - Convert.ToDecimal(spnDiscountAmount.EditValue) + Convert.ToDecimal(spnVATAmount.EditValue);
        }

        private void RecalculateVATAmount()
        {
            decimal rate = Convert.ToDecimal(spnVATRate.EditValue);
            decimal discountAmount = Convert.ToDecimal(spnDiscountAmount.EditValue);
            spnVATAmount.EditValue = (TotalAmountBeforeVAT - discountAmount) * rate / 100;
        }
        private void RecalculateDiscountAmount()
        {
            decimal rate = Convert.ToDecimal(spnDiscountRate.EditValue);
            spnDiscountAmount.EditValue = TotalAmountBeforeVAT * rate / 100;            
        }

        private void BindControlToData()
        {
            TotalAmount = Convert.ToDecimal(txtTotalAmount.EditValue);
            TotalAmountBeforeVAT = Convert.ToDecimal(txtTotalAmoutBeforeVAT.EditValue);
            DiscountAmount = Convert.ToDecimal(spnDiscountAmount.EditValue);
            DiscountRate = Convert.ToDecimal(spnDiscountRate.EditValue);
            VATRate = Convert.ToDecimal(spnVATRate.EditValue);          
 
        }
        #endregion

        #region Event Handlers
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                BindControlToData();
                if(SubmittedForm != null)
                {
                    DiscountAndVATArgs arg = new DiscountAndVATArgs(TotalAmountBeforeVAT, TotalAmount ,DiscountAmount,DiscountRate ,VATRate, VATAmount);
                    SubmittedForm(sender, arg);
                }
                this.Close();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }            
        }

        private void spnVATRate_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (needToCalculateVATAmount)
                {
                    RecalculateVATAmount();
                }
                ReCalculateTotalAmount();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }    
        }

        private void spnDiscountRate_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (needToCalculateDiscountAmount)
                {
                    needToCalculateVATRate = false;
                    needToCalculateDiscountRate = false;                    
                    RecalculateDiscountAmount();                    
                    RecalculateVATAmount();                    
                }
                ReCalculateTotalAmount();
                needToCalculateVATRate = true;
                needToCalculateDiscountRate = true;
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }    
        }

        private void spnVATAmount_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (needToCalculateVATRate)
                {
                    needToCalculateVATAmount = false;
                    needToCalculateDiscountRate = false;
                    decimal VATAmount = Convert.ToDecimal(spnVATAmount.EditValue);
                    decimal discountAmount = Convert.ToDecimal(spnDiscountAmount.EditValue);
                    spnVATRate.EditValue = (VATAmount / (TotalAmountBeforeVAT - discountAmount)) * 100;
                    RecalculateDiscountAmount();
                }
                ReCalculateTotalAmount();
                needToCalculateVATAmount = true;
                needToCalculateDiscountRate = true;
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }    
        }

        private void spnDiscountAmount_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (needToCalculateDiscountRate)
                {
                    needToCalculateDiscountAmount = false;
                    needToCalculateVATRate = false;
                    decimal discountAmount = Convert.ToDecimal(spnDiscountAmount.EditValue);
                    decimal VATAmount = Convert.ToDecimal(spnVATAmount.EditValue);
                    spnDiscountRate.EditValue = (discountAmount / (TotalAmountBeforeVAT + VATAmount)) * 100;
                    RecalculateVATAmount();
                }
                ReCalculateTotalAmount();
                needToCalculateDiscountAmount = true;
                needToCalculateVATRate = true;
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }    
        }

        private void FrmCalculateDiscountAndVAT_Load(object sender, EventArgs e)
        {
            try
            {
                SetDefaultValue();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
                        
    }
}